package com.briup.pts.web.controller;

import com.briup.pts.config.response.Result;
import com.briup.pts.domain.bean.QuarantineRegistration;
import com.briup.pts.service.QuarantineService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 质检管理
 */
// 在Controller类上添加 @Validated注解表示本类方法的参数需要进行参数校验
@Tag(name = "质检管理")
@Validated
@RequestMapping("/QuarantineRegistration")
@RequiredArgsConstructor
@RestController
public class QuarantineController {
    private final QuarantineService service;

    @Operation(summary = "分页模块")
    @GetMapping("/query")
    public Result<?> getQuarantinePage(
            @NotNull(message = "分页参数不能为空")
            @Min(value = 1L, message = "当前页面不能小于1") Integer pageNum,
            @Min(value = 1L, message = "页面大小不能小于1")
            @NotNull(message = "分页参数不能为空") Integer pageSize,
            String bQualified, String grMechanism) {
        // 调用service层方法，时间分页多条件查询
        PageInfo<QuarantineRegistration> pageInfo = service
                .pageQueryByCondition(pageNum, pageSize, bQualified, grMechanism);
        return Result.success(pageInfo)
                .apply("获取成功");
    }

    @Operation(summary = "新增/更新模块")
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(@RequestBody QuarantineRegistration registration) {
        service.saveOrUpdate(registration);
        return Result.success(null)
                .apply("操作成功");
    }

    @Operation(summary = "删除模块")
    @DeleteMapping("/deleteById/{grId}")
    public Result<?> deleteById(
            @NotNull(message = "质检id必须存在")
            @Min(value = 1L, message = "质检id必须有效")
            @PathVariable Integer grId) {
        service.deleteById(grId);
        return Result.success(null)
                .apply("删除成功");
    }

    @Operation(summary = "批量删除检疫记录")
    @DeleteMapping("/deleteByIdAll")
    public Result<QuarantineRegistration> deleteByIdAll(
            @NotEmpty(message = "质检id必须传输")
            @RequestBody List<Integer> ids) {
        service.deleteByIdAll(ids);
        return Result.success();
    }

}
